/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ericsson.nfc.signing;

//#ifdef MyJSR257
//# import my.javax.microedition.contactless.ndef.NDEFRecord;
//#else
import javax.microedition.contactless.ndef.NDEFRecord;
//#endif

/**
 *
 * @author emarkki
 */
class Canonicalizer {
    
    public static byte[] toCanonicalizedForm(NDEFRecord[] records, int[] indexes) {
        byte[][] bytes = new byte[indexes.length][];
        int length = 0;
        for(int i = 0; i < indexes.length; i++) {
            bytes[i] = toCanonicalizedForm(records[indexes[i]]);
            length += bytes[i].length;
        }
        byte[] result = new byte[length];
        for(int i = 0, offset = 0; i < bytes.length; i++) {
            System.arraycopy(bytes[i], 0, result, offset, bytes[i].length);
            
            // Handle MB,ME
            //if(indexes[i] == 0) {
            //    result[offset] |= 0x80; // MB=1
            //    result[offset] &= 0xBF; // ME=0
            //} else if(indexes[i] == records.length-1) {
            //    result[offset] &= 0x7F; // MB=0
            //    result[offset] |= 0x40; // ME=1
            //}
            
            result[offset] |= 0x80; //MB=1
            result[offset] |= 0x40; //ME=1
            
            offset += bytes[i].length;
        }
        
        return result;
    }
        
    static byte[] toCanonicalizedForm(NDEFRecord record) {
        byte[] originalBytes = record.toByteArray();
        byte[] result;
        
        byte[] payloadLengthHighBytes = new byte[3]; // TODO: remove?
        
        
        if((originalBytes[0] & 0x10) == 0) { // SR=0
            payloadLengthHighBytes[0] = originalBytes[2];
            payloadLengthHighBytes[1] = originalBytes[3];
            payloadLengthHighBytes[2] = originalBytes[4];
            
            if((originalBytes[0] & 0x08) == 0) { // IL=0
                result = new byte[originalBytes.length+1];
                result[0] = (byte) (originalBytes[0] & 0xEF);
                result[0] |= 0x08;
                result[1] = originalBytes[1];
                result[2] = originalBytes[2];
                result[3] = originalBytes[3];
                result[4] = originalBytes[4];
                result[5] = originalBytes[5]; 
                result[6] = 0;
                System.arraycopy(originalBytes, 6, result, 7, originalBytes.length-6);
            } else {
               // OK
               result = originalBytes;
            }
            
        } else {
            
            if((originalBytes[0] & 0x08) == 0) { // IL=0
                result = new byte[originalBytes.length+4];
                result[0] = (byte) (originalBytes[0] & 0xEF);
                result[0] |= 0x08;
                result[1] = originalBytes[1];
                result[2] = payloadLengthHighBytes[0];
                result[3] = payloadLengthHighBytes[1];
                result[4] = payloadLengthHighBytes[2];
                result[5] = originalBytes[2];
                result[6] = 0;
                System.arraycopy(originalBytes, 3, result, 7, originalBytes.length-3);
            } else {
                result = new byte[originalBytes.length+3];
                result[0] = (byte) (originalBytes[0] & 0xEF);
                result[1] = originalBytes[1];
                result[2] = payloadLengthHighBytes[0];
                result[3] = payloadLengthHighBytes[1];
                result[4] = payloadLengthHighBytes[2];
                System.arraycopy(originalBytes, 2, result, 5, originalBytes.length-2);
            }
        }
        
        return result;
    }

    public static byte[] toCanonicalizedForm(NDEFRecord[] records, int beginIndex, int endIndex) {
        int[] indexes = new int[endIndex-beginIndex];
        for(int i = 0; i < indexes.length; i++) {
            indexes[i] = beginIndex + i;
        }
        return toCanonicalizedForm(records, indexes);
    }
}
